package com.me.batm.input;

import java.util.Collections;
import java.util.Iterator;
import java.util.Stack;

import com.me.batm.GameConstants;
import com.me.batm.GameConstants.Direction;

public class InputStatus {
	public boolean isLeft,isRight,isUp,isDown;
	
	public boolean isSpace;
	public boolean isPause;
	public boolean isMenu;
	
	public boolean isWin;
	public boolean isDeath;
	public boolean isTele;
	
	Stack<Direction> directions;
	
	public InputStatus(){
		isLeft = false;
		isRight = false;
		isUp = false;
		isDown = false;
		
		isSpace = false;		
		isPause = false;
		isMenu = false;
		
		isTele = false;
		
		directions = new Stack<GameConstants.Direction>();
	}
	
	private void processStack(){
		isLeft = false;
		isRight = false;
		isUp = false;
		isDown = false;
		if(directions.empty())
			return;
		if(directions.peek() == Direction.DOWN)isDown = true;
		if(directions.peek() == Direction.UP)isUp = true;
		if(directions.peek() == Direction.LEFT)isLeft = true;
		if(directions.peek() == Direction.RIGHT)isRight = true;
		
	}
	
	private void deleteFromStack(Direction directionToDelete){
		Collections.reverse(directions);
		for(Iterator<Direction> it = directions.iterator();it.hasNext();){
			Direction dir = it.next();
			if(dir == directionToDelete){
				it.remove();
				break;
			}	
		}
		Collections.reverse(directions);
		processStack();
	}
	
	public void pressLeft(){
		directions.add(Direction.LEFT);
		processStack();
	}
	
	public void pressRight(){
		directions.add(Direction.RIGHT);
		processStack();
	}
	
	public void pressUp(){
		directions.add(Direction.UP);
		processStack();
	}
	
	public void pressDown(){
		directions.add(Direction.DOWN);
		processStack();
	}
	
	public void pressPause() {
		isPause = !isPause;
	}

	public void pressMenu() {
		isMenu = !isMenu;
	}
	public void releaseUp(){deleteFromStack(Direction.UP);}
	public void releaseDown(){deleteFromStack(Direction.DOWN);}
	public void releaseLeft(){deleteFromStack(Direction.LEFT);}
	public void releaseRight(){deleteFromStack(Direction.RIGHT);}
	
	public void pressSpace(){isSpace = true;}
	public void releaseSpace(){isSpace = false;}
	
	public void pressWin(){isWin = true;}
	public void releaseWin(){isWin = false;}
	
	public void pressDeath(){isDeath = true;}
	public void releaseDeath(){isDeath = false;}
	
	public void pressTele(){isTele = true;}
	public void releaseTele(){isTele = false;}
}